#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：V2 
@File    ：__init__.py.py
@IDE     ：PyCharm 
@Author  ：郭星
@Date    ：2025/11/4 20:28 
'''
import json

from DSB.SWT1.Code2.Test.T1 import GasSchedulingInstanceGeneratorExtended, compare_algorithms_on_instances, \
    GeneticAlgorithm, SimulatedAnnealing, TabuSearch, ImprovedTabuSearch, plot_convergence_curves

# from DSB.SWT1.Code2.Test.T1 import compare_algorithms_on_instances
# 1. 生成从小到大的算例
generator = GasSchedulingInstanceGeneratorExtended()
# 生成算例（每个规模3个重复，保存至 ./scaled_instances）
generator.generate_batch_instances(save_dir="./scaled_instances", num_repeats=3)

# 2. 比较四种算法性能
results_df = compare_algorithms_on_instances(generator, instance_dir="./scaled_instances", save_results=True)

# 3. 单独运行一个算例并绘制收敛曲线
# 加载单个算例
instance_path = "./scaled_instances/instance_w10_c4_f2_m2_a3_r0.json"
with open(instance_path, 'r') as f:
    instance = json.load(f)
# 初始化算法
algorithms = [
    GeneticAlgorithm(instance),
    SimulatedAnnealing(instance),
    TabuSearch(instance),
    ImprovedTabuSearch(instance)
]
# 运行并绘制收敛曲线
for algo in algorithms:
    algo.optimize()
plot_convergence_curves(algorithms)

# 4. 查看结果统计
print("\n各算法平均性能：")
print(results_df.groupby("算法").agg({
    "最优完成时间": "mean",
    "运行时间(s)": "mean"
}).round(2))